<?php
/*********************************************
 *  CPG Dragonfly™ CMS
 *********************************************
	Copyright © since 2010 by CPG-Nuke Dev Team
	http://dragonflycms.org

	Dragonfly is released under the terms and conditions
	of the GNU GPL version 2 or any later version
*/

abstract class Dragonfly_Identity_Validate
{

	# Check to see if the username has been taken, or if it is disallowed.
	# Used for registering, changing names, and posting anonymously with a username
	public static function nickname($username)
	{
		global $db, $MAIN_CFG;
		if (50 < strlen($username)) { throw new Exception(_NICK2LONG); }
		if ( 4 > strlen($username)) { throw new Exception(_NICK2SHORT); }
		if (is_numeric($username))  { throw new Exception(_NICKNUMERIC); }
		if (preg_match('~[ *#%"\'`&^@><\\\\/]~',$username))
		{
			throw new Exception(sprintf(_ERROR_BAD_CHAR, _NICKNAME));
		}

		$deniedusers = 'staff|'.$MAIN_CFG->member->DeniedUserNames.'|'.$MAIN_CFG->global->CensorList;
		$words = array();
		if (preg_match('#('.$deniedusers.')#i', $username, $words))
		{
			throw new Exception(_NAMEDENIED." '{$words[0]}'");
		}

		if ($db->count('users', "username='$username'")
		 || $db->count('users_temp', "username='$username'"))
		{
			throw new Exception(_NICKTAKEN);
		}

		return true;
	}

}
